package com.hoyo.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.hoyo.domain.Bullet;
import com.hoyo.domain.BulletReport;
import com.hoyo.domain.ReportItem;

@Mapper
public interface BulletMapper {
	@Insert("insert into danmuku(movie_id, appeared_time, danmu_type, color,user_id, content) "
			+ "values(#{movieId}, #{appearedTime}, #{danmuType}, #{color}, #{userId}, #{content})")
	void save(Bullet bullet) throws Exception;
	
	@Insert("insert into danmu_report(user_id, danmu_id, description) values(#{userId}, #{danmuId}, #{description})")
	void saveReport(BulletReport br);
	
	@Select("select * from danmuku where movie_id=#{0} and is_reported=false")
	List<Bullet> findBulletsByMovieId(int movieId) throws Exception;
	
	@Select("select dmr.id, dmk.content, dmr.description, ui1.user_id, ui1.nickname user_nickname, ui2.user_id be_reported_user_id, "
			+ "ui2.nickname be_reported_user_nickname, (select count(*) from danmu_report where is_solved =#{0}) total "
			+ "from danmu_report dmr left join user_info ui1 "
			+ "on ui1.user_id = dmr.user_id left join danmuku dmk on dmk.id=dmr.danmu_id left join user_info ui2 "
			+ "on ui2.user_id = dmk.user_id where dmr.is_solved =#{0} order by dmr.create_time desc limit #{1},#{2}")
	List<ReportItem> findBulletReportsByCheckOrderByCreateTimeDescLimitBy(boolean checked, int offset, int count) throws Exception;
	
	@Update("update danmu_report set is_solved=#{1}, is_really=#{2} where id=#{0}")
	void updateIsSolvedAndIsReallyById(int id, boolean isSolved, boolean isReally) throws Exception;
	
	/**
	 * 查询指定位置，指定数目的弹幕
	 * @param offset
	 * @param count
	 * @return
	 * @throws Exception
	 */
	@Select("select dmk.*, ui.nickname, mci.danmu_amount total "
			+ "from danmuku dmk left join user_info ui on ui.user_id=dmk.user_id "
			+ "left join movie mv on mv.id=dmk.movie_id " 
			+ "left join movie_count_info mci on mci.movie_id=mv.id "
			+ "limit #{0},#{1}")
	List<Bullet> findBulletsLimitBy(int offset, int count) throws Exception;
	
	@Update("update danmuku set is_reported=#{1} where id=#{0}")
	void updateIsReportById(int id, boolean isReported);
	
	@Select("select * from danmuku where id=#{0}")
	Bullet findBulletById(int id);
	
	@Select("select danmu_id from danmu_report where id=#{0}")
	int findBulletIdByReportId(int id);
	
	@Update("delete from danmuku where id=#{0}")
	void deleteById(int id);
}












